import axios from '@/api'
import { formatTree } from '@/utils/tools';
import { MenuTypeEnum } from '@/common/enum'

export interface IPower {
  power_id: string;
  power_nam: string;
  power_up_id: string;
  power_web_url: string;
  power_index: string;
  is_menu: string;
  key: string;
}

export interface IPowerGroup {
  pow_gp_id: string;
  pow_gp_nam: string;
  power_ids: string;
}

class PowerServices {
  constructor() { }

  /**
   * 获取权限列表
   *
   * @returns
   * @memberof PowerServices
   */
  async getPowerList(types: Array<MenuTypeEnum> = [MenuTypeEnum.MENU, MenuTypeEnum.BUTTON]) {
    const res = await axios.post<unknown, Api.ResponseOptions<IPower[]>>('/btsys/power/get-power-list')
    res.data = formatTree(types.length === 2 ? res.data : res.data.filter((item: IPower) => types.includes(+item.is_menu)), { id: 'power_id', parentId: 'power_up_id' })
    return res
  }

  /**
   * 获取下级权限列表
   *
   * @param {Pick<IPower, 'power_id'>} data
   * @param {Array<MenuTypeEnum>} [types=[MenuTypeEnum.MENU, MenuTypeEnum.BUTTON]]
   * @returns
   * @memberof PowerServices
   */
  async getPowerNextList(data: Pick<IPower, 'power_id'>, types: Array<MenuTypeEnum> = [MenuTypeEnum.MENU, MenuTypeEnum.BUTTON]) {
    const res = await axios.post<unknown, Api.ResponseOptions<IPower[]>>('/btsys/power/get-next-powers', data)
    res.data = types.length === 2 ? res.data : res.data.filter((item: IPower) => types.includes(+item.is_menu))
    return res
  }

  /**
   * 编辑/添加权限
   *
   * @param {IAdmin} data
   * @returns
   * @memberof PowerServices
   */
  updatePower(data: IPower) {
    return axios.post<unknown, Api.ResponseOptions>('/btsys/power/edit-power', data)
  }

  /**
   * 删除权限
   *
   * @param {string} id
   * @returns
   * @memberof PowerServices
   */
  deletePower(data: Pick<IPower, 'power_id'>) {
    return axios.post<unknown, Api.ResponseOptions>('/btsys/power/del-power', data)
  }

  /**
   * 获取权限组列表
   *
   * @returns
   * @memberof PowerServices
   */
  getPowerGroupList() {
    return axios.post<unknown, Api.ResponseOptions<IPowerGroup[]>>('/btsys/power/get-power-group-list')
  }

  /**
   * 编辑/添加权限组
   *
   * @param {IAdmin} data
   * @returns
   * @memberof PowerServices
   */
  updatePowerGroup(data: IPowerGroup) {
    return axios.post<unknown, Api.ResponseOptions>('/btsys/power/edit-power-group', data)
  }

  /**
   * 删除权限组
   *
   * @param {string} id
   * @returns
   * @memberof PowerServices
   */
  deletePowerGroup(data: Pick<IPowerGroup, 'pow_gp_id'>) {
    return axios.post<unknown, Api.ResponseOptions>('/btsys/power/del-power-group', data)
  }
}

export const poserServices = new PowerServices()

export default PowerServices